var $ = require("../../utils/Ajax.js");



let disp = require("../../utils/broadcast");
var WebIM = require("../../utils/WebIM")["default"];


Page({
  data: {
    // 用户信息
    UserInfor: null,

    // 是否显示登录页面
    LoginBol: true,

    // 是否同意协议
    ProtocolBol: true,

    // 是否显示直播按钮
    IsAnchor: false,

    // 控制模态框
    BgModule: false,
    // 模态框动画速度控制
    BgModuleSpeed: 300,

    // 关联列表
    Correlation: [],

    // 选择的关联ID
    CorrelationId: 0
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的"
    }) 
  },

  onShow: function () {
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    console.log(UserInfor);
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      this.setData({
        LoginBol: true
      })
    }else{
      var that= this;
      $.Ajax("api/uindex", "GET", {}, function (res) {
        if (res.s) {
          if (res.d.user.isAnchor == 1) {
            that.setData({
              IsAnchor: true
            })
          }
          that.setData({
            LoginBol: false,
            UserInfor: res.d.user
          })
        }
      })
    }
  },

  // 是否选中登录对勾
  LoginStatus: function (e) {
    var Status = e.detail.value[0];
    if (Status) {
      this.setData({
        ProtocolBol: true
      })
    } else {
      this.setData({
        ProtocolBol: false
      })
    }
  },

  // 用户点击登录
  ClickLogin: function(){
    var that = this;
    wx.login({
      success: function (res) {
        var Code = res.code;
        wx.getUserInfo({
          success: function (res_) {
            $.Ajax("api/login", "POST", {
              encryptData: res_.encryptedData,
              iv: res_.iv,
              code: Code
            }, function (res) {
              if (res.s) {
                wx.setStorageSync('Token', 'Bearer ' + res.d.token);
                $.Ajax("api/uindex","GET",{},function(res){
                  if(res.s){
                    if (res.d.user.isAnchor == 1){
                      that.setData({
                        IsAnchor: true
                      })
                    }
                    that.setData({
                      LoginBol: false ,
                      UserInfor: res.d.user
                    })
                    wx.setStorageSync('UserInfor', res.d.user);
                    wx.setStorage({
                      key: "myUsername",
                      data: res.d.user.openId.toLowerCase(),
                    });
                  //  -----------------------------------------------------
                    var options = {
                      apiUrl: WebIM.config.apiURL,
                      username: that.data.UserInfor.openId.toLowerCase(),
                      password: "123456",
                      nickname: that.data.UserInfor.nickName,
                      appKey: WebIM.config.appkey,
                      success: function (res) {
                        console.log('res', res)
                        if (res.statusCode == "200") {
                          console.log('注册成功');
                          var data = {
                            apiUrl: WebIM.config.apiURL,
                            user: that.data.UserInfor.openId.toLowerCase(),
                            pwd: "123456",
                            grant_type: "password",
                            appKey: WebIM.config.appkey
                          };
                         
                        }
                      },
                      error: function (res) {
                        console.log('res', res)
                        if (res.statusCode !== "200") {
                          if (res.statusCode == '400' && res.data.error == 'illegal_argument') {
                            return console.log('用户名非法!')
                          }else{
                            //登录
                            getApp().conn.open({
                              apiUrl: WebIM.config.apiURL,
                              user: that.data.UserInfor.openId.toLowerCase(),
                              pwd: "123456",
                              grant_type: "password",
                              appKey: WebIM.config.appkey
                            });
                          }
                        }
                      }
                    };
                    WebIM.utils.registerUser(options);
                  //  --------------------------------------------
                    console.log("registerUser")
                  }else{
                    wx.showToast({
                      title: res.m,
                      icon: 'none',
                      duration: 200000
                    })
                  }
                })
              } else {
                that.setData({
                  LoginBol: true  
                })
                wx.showToast({
                  title: res.m,
                  icon: 'none',
                  duration: 200000
                })
              }
            });
          }
        })
      }
    });
  },

  // 点击修改个人信息
  ModifyInfor: function(e){
    var Id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../ModifyInfor/ModifyInfor?Id=' + Id,
    })
  },

  // 点击 - 好友
  MyFriends: function () {
    wx.navigateTo({
      url: '../MyFriends/MyFriends',
    })
  },

  // 点击 - 关注
  MyAttention: function () {
    wx.navigateTo({
      url: '../MyAttention/MyAttention',
    })
  },

  // 点击 - 粉丝
  MyFans: function () {
    wx.navigateTo({
      url: '../MyFans/MyFans',
    })
  },

  // 点击我的订单
  MyOrder: function (e) {
    var Status = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../MyOrder/MyOrder?Status=' + Status,
    })
  },

  // 点击退款售后
  MyRefund: function(){
    wx.navigateTo({
      url: '../MyRefund/MyRefund',
    })
  },

  // 点击我的收藏
  MyCollection: function(){
    wx.navigateTo({
      url: '../MyCollection/MyCollection',
    })
  },

  // 点击我的存茶
  MyTea: function(){
    wx.navigateTo({
      url: '../MyTea/MyTea',
    })
  },

  // 点击我的钱包
  MyWallet: function(){
    wx.navigateTo({
      url: '../MyWallet/MyWallet',
    })
  },

  // 点击收货地址
  ShippingAddress: function () {
    wx.navigateTo({
      url: '../ShippingAddress/ShippingAddress',
    })
  },

  // 点击关于我们
  AboutUs: function(){
    wx.navigateTo({
      url: '../AboutUs/AboutUs',
    })
  },
  
  // 点击隐私设置
  // SetUp: function () {
  //   wx.navigateTo({
  //     url: '../SetUp/SetUp',
  //   })
  // },

  // 点击隐私设置
  PrivacySetting: function(){
    wx.navigateTo({
      url: '../PrivacySetting/PrivacySetting',
    })
  },
   
  // 点击交友设置
  FriendsSetting: function(){
    wx.navigateTo({
      url: '../FriendsSetting/FriendsSetting',
    })
  },

  // 点击直播预告
  PlayTrailer: function(){
    wx.navigateTo({
      url: '../PlayTrailer/PlayTrailer',
    })
  },

  // 点击开始直播
  PlayLive: function (e) {
    var that = this
    var addLiveVideoNext = wx.getStorageSync('addLiveVideoNext');
    console.log(addLiveVideoNext)
    if (addLiveVideoNext.length < 1){
      wx.showToast({
        title: '请先设置直播预告',
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }else{
      var BgModuleSpeed = that.data.BgModuleSpeed;
      var BgModule = e.currentTarget.dataset.bgmodule;
      if (BgModule == "open") {
        that.setData({
          BgModule: true,
          Correlation: addLiveVideoNext,
          ScrollY: false
        });
        var animation = wx.createAnimation({
          duration: BgModuleSpeed
        });
        animation.opacity(1).scale(1, 1).step();
        that.setData({
          animationData: animation.export()
        })
      } else {
        var animation = wx.createAnimation({
          duration: BgModuleSpeed
        });
        animation.opacity(0).scale(2, 2).step();
        that.setData({
          animationData: animation.export()
        })
        setTimeout(function () {
          that.setData({
            BgModule: false,
            ScrollY: true,
            CorrelationId: 0
          });
        }, BgModuleSpeed)
      }
    }
  },

  // 选择某个直播的预告
  CorrelationChange: function(e){
    var CorrelationId = e.detail.value.split("_");
    this.setData({
      CorrelationId: CorrelationId[0]
    })
    var weChatMsgAllUser = CorrelationId[1] + '_' + CorrelationId[2] + '_' + CorrelationId[3] + '_' +CorrelationId[4];
    wx.setStorageSync('weChatMsgAllUser', weChatMsgAllUser);
  },

  // 点击确定
  CorrelationOk: function(){
    var CorrelationId = Number(this.data.CorrelationId);
    console.log(CorrelationId)
    if (CorrelationId == 0){
      wx.showToast({
        title: '请选择需要关联的主题',
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }else{
      wx.navigateTo({
        url: '../PlayLive/PlayLive?CorrelationId=' + this.data.CorrelationId,
      })
    }
  },

  FromId: function(e){
    $.Ajax("api/addFromId","GET",{
      fromId: e.detail.formId
    },function(res){
      console.log(res)
    })
  },

  xieyizhengce: function(e){
    var Id = e.currentTarget.dataset.id;
    $.Ajax("api/about", "GET", {
      id: Id
    }, function (res) {
      if (res.s) {
        wx.setStorageSync('About', res.d.about.content);
        wx.navigateTo({
          url: '../AboutUs_/AboutUs_',
        })
      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
      }
    })
  }
})